//
//  UIView+Common.m
//  jhjr
//
//  Created by winter on 15/12/29.
//  Copyright © 2015年 DJDG. All rights reserved.
//

#import "UIView+Common.h"
#import "MBProgressHUD.h"
#import <objc/runtime.h>
#import <AVFoundation/AVFoundation.h>

#define screen_Bounds [UIScreen mainScreen].bounds
#define screen_Height [UIScreen mainScreen].bounds.size.height
#define screen_Width  [UIScreen mainScreen].bounds.size.width

@implementation UIView (Common)

static char LoadingViewKey;

- (void)setLoadingView:(WDLoadingView *)loadingView
{
    [self willChangeValueForKey:@"LoadingViewKey"];
    objc_setAssociatedObject(self, &LoadingViewKey, loadingView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:@"LoadingViewKey"];
}

- (WDLoadingView *)loadingView
{
    return objc_getAssociatedObject(self, &LoadingViewKey);
}

- (void)beginLoadingWithTip:(NSString *)tip
{
    if (!self.loadingView) {
        self.loadingView = [[WDLoadingView alloc] initWithFrame:CGRectMake(0, -30, screen_Width, screen_Height-64)];
    }
    [self addSubview:self.loadingView];
    [self.loadingView startLoadingWithTip:tip];
}

- (void)beginLoading
{
    [self beginLoadingWithTip:nil];
}

- (void)endLoading
{
    if (self.loadingView) {
        [self.loadingView stopLoading];
        [self.loadingView removeFromSuperview];
        self.loadingView = nil;
    }
}

@end

@interface WDLoadingView ()
/**  */
@property (nonatomic, strong) MBProgressHUD *hud;

@end

@implementation WDLoadingView

- (void)startLoadingWithTip:(NSString *)tip
{
    if (nil == self.hud) {
        self.hud = [[MBProgressHUD alloc] initWithView:self];
        self.hud.graceTime = 0.5;
        self.hud.minShowTime = 1;
        self.hud.removeFromSuperViewOnHide = YES;
        [self addSubview:self.hud];
        self.hud.label.text = tip.length > 0 ? tip : @"正在加载...";
        [self.hud showAnimated:YES];
    }
}

- (void)stopLoading
{
    if (self.hud) {
        [self.hud hideAnimated:YES];
        self.hud = nil;
    }
}
@end

@implementation UIView (Custom)

- (void)removeAllSubViews
{
    for (UIView *view in self.subviews) {
        [view removeAllSubViews];
        [view removeFromSuperview];
    }
}

// 摇动
- (void)shake
{
    CAKeyframeAnimation *keyframeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
    CGFloat s = 16;
    keyframeAnimation.values = @[
                                 @(-s),
                                 @(0),
                                 @(s),
                                 @(0),
                                 @(-s),
                                 @(0),
                                 @(s),
                                 @(0)
                                 ];
    
    //时长
    keyframeAnimation.duration = .3f;
    
    //重复
    keyframeAnimation.repeatCount = 1;
    
    //移除
    keyframeAnimation.removedOnCompletion = YES;
    // 震动
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    
    [self.layer addAnimation:keyframeAnimation forKey:@"shake"];
}

@end
